<!DOCTYPE html>
<html><head>
	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
	<meta charset="utf-8">
	<meta name="viewport" content="initial-scale=1, width=device-width, maximum-scale=1, user-scalable=no">
	<meta name="viewport" content="initial-scale=1.0,user-scalable=no,maximum-scale=1" media="(device-height: 568px)">
	<meta name="apple-mobile-web-app-capable" content="yes">
	<meta name="apple-touch-fullscreen" content="yes">
	<meta name="full-screen" content="yes">
	<title>硬件JSAPI测试</title>
	<link rel="stylesheet" type="text/css" href="h1/device.css">
	<script type="text/javascript" src="js/jquery-1.10.1.min.js"></script>
<script type="text/javascript" src="js/QueryStringParser.js"></script>
<script type="text/javascript" src="js/jquery.json.min.js"></script>  
<script type="text/javascript" src="js/jsonMsg.js"></script>  
</head>
<body>
	<div id="debug"></div>
	<div id="buttons">
		<div>
			<a href="javascript:void(0)" class="button" id="openWXDeviceLib">API初始化</a>
			<a href="javascript:void(0)" class="button" id="closeWXDeviceLib">API释放</a>
		</div>
		<div>
			<a href="javascript:void(0)" class="button" id="getWXDeviceInfos">设备状态</a>
			<a href="javascript:void(0)" class="button" id="getWXDeviceBindTicket">绑定票据</a>
			<a href="javascript:void(0)" class="button" id="getWXDeviceUnbindTicket">解绑票据</a>
			<a href="javascript:void(0)" class="button" id="setSendDataDirection">设置SendData目标</a>
		</div>
		<div>
			<a href="javascript:void(0)" class="button" id="startScanWXDevice">开始扫描</a>
			<a href="javascript:void(0)" class="button" id="stopScanWXDevice">停止扫描</a>
		</div>
		<div>
			<a href="javascript:void(0)" class="button" id="connectWXDevice">连接设备</a>
			<a href="javascript:void(0)" class="button" id="disconnectWXDevice">断开设备</a>
			<a href="javascript:void(0)" class="button" id="sendDataToWXDevice">发送数据</a>
		</div>
	</div>

	<script src="h1/debug.js" type="text/javascript"></script>
	
	<script src="http://res.wx.qq.com/open/js/jweixin-1.0.0.js"></script>
<script>
	var pageURL = location.href.split('#')[0];
	//alert(pageURL);
	var openId = queryString.getParam("openid");
	$(document).ready(function(){
  	

		var dataStr = {
			PageURL: 	pageURL
		};	
		var bodyStr = popBody('JSSignature',dataStr);
		var request = popRequest('JSSignature',openId,bodyStr);
		//调用了jquery.json 库  
		var encoded = $.toJSON( request );  
		var jsonStr = encoded;  

		$.ajax({  
		url : actionStr,  
		type : 'POST',  
		data : jsonStr,  
		dataType : 'json',  
		contentType : 'application/json',  
		success : function(data, status, xhr) {  
		//         Do Anything After get Return data  
		        var timestamp = data.portalResp.Data.timestamp;
		        var noncestr = data.portalResp.Data.noncestr;
		        var signature = data.portalResp.Data.signature;
		        var appId = data.portalResp.Data.appId;  
		        
		         /*
					   * 注意：
					   * 1. 所有的JS接口只能在公众号绑定的域名下调用，公众号开发者需要先登录微信公众平台进入“公众号设置”的“功能设置”里填写“JS接口安全域名”。
					   * 2. 如果发现在 Android 不能分享自定义内容，请到官网下载最新的包覆盖安装，Android 自定义分享接口需升级至 6.0.2.58 版本及以上。
					   * 3. 常见问题及完整 JS-SDK 文档地址：http://mp.weixin.qq.com/wiki/7/aaa137b55fb2e0456bf8dd9148dd613f.html
					   *
					   * 开发中遇到问题详见文档“附录5-常见错误及解决办法”解决，如仍未能解决可通过以下渠道反馈：
					   * 邮箱地址：weixin-open@qq.com
					   * 邮件主题：【微信JS-SDK反馈】具体问题
					   * 邮件内容说明：用简明的语言描述问题所在，并交代清楚遇到该问题的场景，可附上截屏图片，微信团队会尽快处理你的反馈。
					   */
					  wx.config({
					  		beta: true, // 开启内测接口调用，注入wx.invoke方法
					      debug: true,
					      appId: appId,
					      timestamp: timestamp,
					      nonceStr: noncestr,
					      signature: signature,
					      jsApiList: [
					      	'hideOptionMenu',
					      	'showOptionMenu',
					        'onMenuShareAppMessage',
					        'openWXDeviceLib',
					        'startScanWXDevice',
					        'getWXDeviceInfos',
					        'onScanWXDevicesResult',
					        'onScanWXDeviceResult',
					        'closeWXDeviceLib',
					        'sendDataToWXDevice',
					        'stopScanWXDevice',
					        'connectWXDevice',
					        'disconnectWXDevice',
					         'getWXDeviceTicket',
					        'configWXDeviceWiFi',
					        'setSendDataDirection'
					      ]
					  });
						
						wx.ready(function () {
							
							//var deviceId = "1A32267BA1221988";
							var deviceId = "1A32267BA1221809";

	var readyFunc = function onBridgeReady() {
		document.querySelector('#openWXDeviceLib').addEventListener('touchstart', function(e){
			WeixinJSBridge.invoke('openWXDeviceLib', {}, function(res){
				console.log('openWXDeviceLib', res);
			});
		});
		
		document.querySelector('#openWXDeviceLib').addEventListener('touchend', function(e){
			alert("touchEnd");
		});
		
		document.querySelector('#closeWXDeviceLib').addEventListener('touchstart', function(e){
			WeixinJSBridge.invoke('closeWXDeviceLib', {}, function(res){
				console.log('closeWXDeviceLib', res);
			});
		});

		document.querySelector('#getWXDeviceInfos').addEventListener('touchstart', function(e){
			WeixinJSBridge.invoke('getWXDeviceInfos', {}, function(res){
				console.log('getWXDeviceInfos', res);
			});
		});
		document.querySelector('#getWXDeviceBindTicket').addEventListener('touchstart', function(e){
			alert("当前deviceId="+deviceId);
			WeixinJSBridge.invoke('getWXDeviceTicket', {"deviceId":deviceId, "type":"1"}, function(res){
				console.log('getWXDeviceTicket', res);
			});
		});
		document.querySelector('#getWXDeviceUnbindTicket').addEventListener('touchstart', function(e){
			WeixinJSBridge.invoke('getWXDeviceTicket', {"deviceId":deviceId, "type":"2"}, function(res){
				console.log('getWXDeviceTicket', res);
			});
		});
		document.querySelector('#setSendDataDirection').addEventListener('touchstart', function(e){
			WeixinJSBridge.invoke('setSendDataDirection', {"deviceId":deviceId, "direction":1 }, function(res){
				console.log('setSendDataDirection', res);
			});
		});

		document.querySelector('#startScanWXDevice').addEventListener('touchstart', function(e){
			WeixinJSBridge.invoke('startScanWXDevice', {"btVersion": "bc"}, function(res){
				console.log('startScanWXDevice', res);
			});
		});
		document.querySelector('#stopScanWXDevice').addEventListener('touchstart', function(e){
			WeixinJSBridge.invoke('stopScanWXDevice', {}, function(res){
				console.log('stopScanWXDevice', res);
			});
		});

		document.querySelector('#connectWXDevice').addEventListener('touchstart', function(e){
			WeixinJSBridge.invoke('connectWXDevice', {"deviceId":deviceId}, function(res){
				console.log('connectWXDevice', res);
			});
		});
		document.querySelector('#disconnectWXDevice').addEventListener('touchstart', function(e){
			WeixinJSBridge.invoke('disconnectWXDevice', {"deviceId":deviceId}, function(res){
				console.log('disconnectWXDevice', res);
			});
		});
		document.querySelector('#sendDataToWXDevice').addEventListener('touchstart', function(e){
			WeixinJSBridge.invoke('sendDataToWXDevice', {"deviceId":deviceId, "base64Data":"Ejl="}, function(res){
				console.log('sendDataToWXDevice', res);
			});
		});

		WeixinJSBridge.on('onWXDeviceBindStateChange', function(argv) {
			console.log('onWXDeviceBindStateChange', argv);
		});
		WeixinJSBridge.on('onWXDeviceStateChange', function(argv) {
			console.log('onWXDeviceStateChange', argv);
		});
		WeixinJSBridge.on('onScanWXDeviceResult', function(argv) {
			console.log('onScanWXDeviceResult', argv);
		});
		WeixinJSBridge.on('onScanWXDevicesResult', function(argv) {
			console.log('onScanWXDevicesResult', argv);
		});
		WeixinJSBridge.on('onReceiveDataFromWXDevice', function(argv) {
			console.log('onReceiveDataFromWXDevice', argv);
		});
		WeixinJSBridge.on('onWXDeviceBluetoothStateChange', function(argv) {
			console.log('onWXDeviceBluetoothStateChange', argv);
		});
	};

	if (typeof WeixinJSBridge === "undefined") {
		document.addEventListener('WeixinJSBridgeReady', readyFunc, false);
	} else {
		readyFunc();
	}
							try{	
								wx.hideOptionMenu();
	
								
								
							}catch(e){
								alert(e);
							}
	
						});
					
						wx.error(function (res) {
						  alert(res.errMsg);
						});
					
		},  
		Error : function(xhr, error, exception) {  
		    // handle the error.    
		    alert(exception.toString());  
		}  
		});  
	

 
});

	

	</script>

</body></html>